import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class App {
    public static void main(String[] args) {
        ArrayList<User> list1 = new ArrayList<>();
        while (true) {
            User user = new User();
            System.out.println("欢迎使用学生管理系统");
            System.out.println("请选择操作\t1.登录\t2.注册\t3.忘记密码");
            Scanner sc = new Scanner(System.in);
            int choose = sc.nextInt();
            switch (choose){
                case 1 -> {
                    Register(list1);
                }
                case 2 -> {
                    SignUp(list1);
                    System.out.println("恭喜你，注册成功");

                }
                case 3 -> ForgotPassword(list1);
                default -> System.out.println("你输入的格式有误，请重新输入");
            }
        }


    }

    //注册
    public static void SignUp(ArrayList<User> list1){
        String UserName = UserNameSignUp(list1);
        String PassWord = PassWord();
        String IdNumber = IdNumberLegitimacy();
        String PhoneNumber = PhoneNumberLegitimacy();
        User u = new User(UserName,PassWord,IdNumber,PhoneNumber);
        list1.add(u);
    }

    //登录
    private static void Register(ArrayList<User> list1){
        Scanner sc = new Scanner(System.in);
        //ExistUserName(ArrayList<User> list1,String UserName)
        while (true) {
            System.out.println("请输入用户名");
            String Username = sc.next();
            if(ExistUserName(list1,Username)) {
                for (int i = 0; i < list1.size(); i++) {
                    User user = list1.get(i);
                    while (true) {
                        if (Username.equals(user.getUserName())) {
                            while (true) {
                                System.out.println("请输入密码");
                                String PassWork = sc.next();
                                if (user.getPassWork().equals(PassWork)) {
                                    while (true) {
                                        System.out.println("请输入验证码");
                                        String date = GenerateVerificationCode();
                                        System.out.println("【"+date+"】");
                                        String VerificationCode = sc.next();
                                        if (VerificationCode.equals(date)) {
                                            System.out.println("恭喜你成功进入学生管理系统");
                                            System.out.println();
                                            System.out.println();
                                            StudentSystem.LogIn ();
                                            //return;
                                            //接入学生管理系统
                                        }else {
                                            System.out.println("输入的验证码有误，请重新输入");
                                        }
                                    }
                                } else {
                                    System.out.println("你输入的密码错误");
                                    System.out.println("请重新输入");
                                }
                            }
                        } else {
                            System.out.println("你输入的用户名不存在");
                            System.out.println("请重新输入");
                        }
                    }
                }
            }else {
                System.out.println("系统内暂该无用户，请注册后再登录");
                System.out.println("----------------------------------------------------------------------------");
                System.out.println();
                System.out.println();
                System.out.println();
                return;
            }
        }
    }

    //忘记密码
    public static void ForgotPassword(ArrayList<User> list1){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入忘记密码的用户名");
        String UserName = sc.next();
        if (ExistUserName(list1,UserName)) {
            int i = getIndex(list1,UserName);
                User user = list1.get(i);
                while (true) {
                    if (ExistUserName(list1,UserName)){
                        while (true) {
                            System.out.println("请输入身份证号码");
                            String IdNumber = sc.next();
                                if (IdNumber.equals(user.getIdNumber())){
                                    while (true) {
                                        System.out.println("请输入手机号码");
                                        String PhoneNumber = sc.next();
                                        if (PhoneNumber.equals(user.getPhoneNumber())){
                                            while (true) {
                                                System.out.println("请输入新密码");
                                                String NewPassNumber1 = sc.next();
                                                System.out.println("请再次输入新密码");
                                                String NewPassNumber2 = sc.next();
                                                if (NewPassNumber1.equals(NewPassNumber2)){
                                                    user.setPassWork(NewPassNumber1);
                                                    System.out.println("密码修改成功，请务必牢记密码");
                                                    list1.add(user);
                                                    return;
                                                }else{
                                                    System.out.println("两次输入的密码不一致，请重新输入");
                                                }
                                            }
                                        }else {
                                            System.out.println("用户名所对应的手机号不符合");
                                            System.out.println("请重新输入正确的手机号");
                                        }
                                    }
                                }else {
                                    System.out.println("用户名所对应的身份证不符合");
                                    System.out.println("请重新输入正确的身份证");
                                }

                        }
                    }else {
                        System.out.println("所输入的用户名不存在");
                        System.out.println("请重新输入用户名");
                    }
                }
        }else {
            System.out.println("系统内暂该无用户，请注册后再登录");
            System.out.println("----------------------------------------------------------------------------");
            System.out.println();
            System.out.println();
            System.out.println();
        }
    }

    //注册1：填写用户名
    public static String UserNameSignUp(ArrayList<User> list1){
        Scanner sc = new Scanner(System.in);
            while (true) {
                System.out.println("请输入要创建的用户名");
                System.out.println("格式要求：长度为3~15位之间，不能为纯数字");
                String UserName = sc.next();
                if (ExistUserName(list1,UserName)){
                    System.out.println("该用户名已被占用");
                    System.out.println();
                    System.out.println();
                    continue;
                }if (UserNameLegitimacy(UserName)){
                    System.out.println("当前用户名可用");
                    return UserName;
                }else {
                    System.out.println("你输入的格式不正确");
                    System.out.println("请重新输入");
                    System.out.println();
                    System.out.println();
                }
            }
    }

    //注册2：输入密码
    public static String PassWord(){
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入密码");
            String PassWord1 = sc.next();
            System.out.println("请再次输入密码");
            String PassWord2 = sc.next();
            if (PassWord1.equals(PassWord2)){
                System.out.println("密码符合，请务必记住密码");
                return PassWord1;
            }else {
                System.out.println("两次密码不相同，请重新输入");
            }
        }

    }

    //注册3：验证身份证
    public static String IdNumberLegitimacy(){
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入身份证号码");
            String IdNumber = sc.next();
            if (IdNumber.length() == 18){
                char c = IdNumber.charAt(0);
                if (c != 0){
                    for (int i = 0; i < IdNumber.length()-1; i++) {
                        char c1 = IdNumber.charAt(i);
                        if (c1 >= '0' && c1 <= '9'){
                            if (IdNumber.charAt(IdNumber.length()-1) == 'x' || IdNumber.charAt(IdNumber.length()-1) == 'X' || (c1 >= '0' && c1 <= '9')){
                                System.out.println("身份证录入成功");
                                return IdNumber;
                            }
                        }
                    }
                }
            }
            System.out.println("身份证号码格式输入不正确");
            System.out.println("请重新输入");
        }

    }

    //注册4：手机号码验证
    public static String PhoneNumberLegitimacy(){
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入手机号码");
            String IdNumber = sc.next();
            if (IdNumber.length() == 11){
                char c = IdNumber.charAt(0);
                if (c != 0){
                    for (int i = 0; i < IdNumber.length(); i++) {
                        if (c >= '0' && c <= '9'){
                                System.out.println("手机号码录入成功");
                                return IdNumber;
                            }
                        }
                    }
                }
                System.out.println("手机号码格式输入不正确");
            System.out.println("请重新输入");
            }
    }


    //生成验证码
    public static String GenerateVerificationCode(){
        char[] VerificationCode = new char[5];
        char[] date = new char[62];
        //写入a~z字符
        for (int i = 0;i < 26;i++){
            date[i] = (char)('a' + i);
            }
        //写入A~Z字符
        for (int i = 26; i < 52; i++) {
            date[i] = (char)('A' + i - 26);
        }
        //写入0~9字符
        for (int i = 52; i < date.length; i++) {
            date[i] = (char)('0' + i - 52);
        }
        Random sj = new Random();
        for (int i = 0; i < VerificationCode.length; i++) {
            VerificationCode[i] = date[sj.nextInt(date.length)];
        }
        //生成完整的验证码
        String VerificationCodeDate = new String(VerificationCode);
        return VerificationCodeDate;
    }

    //判断账户是否存在
    public static boolean ExistUserName(ArrayList<User> list1,String UserName){
        for (int i = 0; i < list1.size(); i++) {
            User stu = list1.get(i);
            String stuUserName = stu.getUserName();
            if (stuUserName.equals(UserName)){
                //在返回true
                return true;
            }
        }
        //不在返回false
        return false;
    }

    //判断用户名格式的合法性【有漏洞】
    public static boolean UserNameLegitimacy(String UserName){
        if (UserName.length() >= 3 && UserName.length() <=15){
            int count = 0;
        for (int i = 0; i < UserName.length(); i++) {
            char c = UserName.charAt(i);
            //仅仅判断存在字母即可通过合法性【漏洞】
             if ((c >= 'a' && c <= 'z')||(c >= 'A' && c <= 'B')){
                 count ++;
             }
        }
        if (count >= 1){
            return true;
        }
    }
        return false;
}

    //查找用户名在集合中的序列
    private static int getIndex(ArrayList<User> list, String UserName){
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            String UserNameDate = user.getUserName();
            if (UserName.equals(UserNameDate)){
                return i;
            }

        }
        return -1;
    }


}
